#include "AI.h"
#include "Level.h"
#include <assert.h>

CAI* CAI::instance=0;

CAI::CAI(void): difficulty(eDifficultyNormal), numberOfLevel(0)
{}

CAI::~CAI(void)
{
	for(vector<CLevel*>::iterator p=this->vectorLevel.begin();p<this->vectorLevel.end();p++)
	{
		delete (*p);
		(*p)=0;
	}
}

CAI& CAI::getInstance(void)
{
	if(instance==0)
	{
		instance= new CAI();
	}
	return *instance;
}

/*function that delete the AI*/
void CAI::Close(void)
{
	delete instance;
	instance=0;
}

/*create a level. indexLevel return the index of the level in the vector*/
void CAI::createNewLevel(int* indexLevel, int numberOfWave)
{
	assert(numberOfWave>0);

	(*indexLevel)=(int)this->vectorLevel.size();
	this->numberOfLevel++;
	CLevel* newLevel=new CLevel();
	for(int i=0;i<numberOfWave;i++)
	{
		newLevel->createWave();
	}
	this->vectorLevel.push_back(newLevel);
}

/*Initialise a wave. Spawn the enemies. If the number of waves intiliased is superior to the number of wave 
declared during the creation of the level, the initialisation gonna be ignored. This mecanism is done during the 
loading from the xml file.*/
void CAI::initialiseWave(int indexWave, int indexLevel, int numberOfEnemies,int typeOfEnemies, int timeToSpawn,
						 int timeToLive, int timeBetweenEnemiesToSpawn)
{
	assert(indexWave>=0);
	assert(indexLevel>=0);
	assert(numberOfEnemies>0);
	assert(typeOfEnemies>=0);
	assert(timeToSpawn>=0);
	assert(timeToLive>=0);
	assert(timeBetweenEnemiesToSpawn>=0);

	this->vectorLevel[indexLevel]->initialiseWave(indexWave,numberOfEnemies, typeOfEnemies, timeToSpawn,
				timeToLive, timeBetweenEnemiesToSpawn);
}

/*Add a waypoint to a wave*/
void CAI::addWayPoint(int indexLevel, int indexWave, CVector2D wayPoint)
{
	assert(indexWave>=0);
	assert(indexLevel>=0);

	this->vectorLevel[indexLevel]->createWayPoint(indexWave,wayPoint);
}

int CAI::getDifficulty(void)const
{
	return this->difficulty;
}
void CAI::setDifficulty(int difficulty)
{
	this->difficulty=difficulty;
}